package tq.com.tqteacher.adapt;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.PicassoProvider;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import tq.com.systools.system.FileHelper;
import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqteacher.R;
import tq.com.tqteacher.adapt.viewHold.TQDonothingViewHold;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.model.TQCropSetsModel;

public class TQCropSetsAdapt extends RecyclerView.Adapter {
    private Context mContext;
    private ArrayList<TQAtachModel> data = new ArrayList<>();

    public TQCropSetsAdapt(Context pContext){
        mContext = pContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        if(pI == 1){
            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_crop_header_layout, pViewGroup, false);
            lView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //add
                    if(mICropListener != null) mICropListener.onChoice();
                }
            });
            return new TQDonothingViewHold(lView);
        }else{

            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_crop_item_layout, pViewGroup, false);
            return new ItemViewHold(lView);

        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        if(pI == 0) return;
        int pos = pI - 1;


        if(pViewHolder instanceof ItemViewHold){
            TQAtachModel item = data.get(pos);
            ((ItemViewHold)pViewHolder).bindData(item);
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0) return  1;
        return 0;
    }

    public ArrayList<TQAtachModel> getData(){
        return data;
    }


    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    public void addItem(TQAtachModel pTQCropSetsModel){
        if(pTQCropSetsModel == null) return;

        int pos = data.size();
        data.add(pTQCropSetsModel);
        notifyDataSetChanged();
    }
    public void replace(ArrayList<TQAtachModel> all){
        if(all == null) return;
        data.clear();
        data.addAll(all);
        notifyDataSetChanged();
    }

    public void deleteItem(TQAtachModel pTQCropSetsModel){
        if(pTQCropSetsModel == null) return;

        int pos = data.indexOf(pTQCropSetsModel);
        data.remove(pos);
        notifyItemRemoved(pos + 1);
    }

    public class ItemViewHold extends RecyclerView.ViewHolder{
        private TQAtachModel mTQCropSetsModel;
        private ImageView mDeleteView;
        private ImageView mContentPicture;


        public ItemViewHold(View v){
            super(v);

            mDeleteView = v.findViewById(R.id.delete);
            mContentPicture = v.findViewById(R.id.picture);
            mDeleteView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mICropListener != null){
                        mICropListener.onDelete(mTQCropSetsModel);
                    }
                }
            });
            mContentPicture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mICropListener != null){
                        mICropListener.onShow(mTQCropSetsModel);
                    }
                }
            });
        }

        public void bindData(TQAtachModel pTQCropSetsModel){
            this.mTQCropSetsModel = pTQCropSetsModel;
            if(mTQCropSetsModel == null) return;

            String path = TQPictureUpload.downloadPath(mTQCropSetsModel.getCover());
            RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.ic_default_picture)
                    .error(R.drawable.ic_default_picture).diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            if(FileHelper.isFileExists(path)){
                Glide.with(mContext).load(new File(path)).apply(lOptions).into(mContentPicture);
            }else{

                Glide.with(mContext).load(path).apply(lOptions).into(mContentPicture);
            }

        }
    }

    private ICropListener mICropListener ;
    public void setCropListener(ICropListener l){
        mICropListener = l;
    }

    public interface ICropListener{
        void onChoice();
        void onDelete(TQAtachModel file);
        void onShow(TQAtachModel file);
    }
}
